package lance.honeydew.action;

import java.util.*;

import lance.honeydew.bean.ItemInformation;
import lance.honeydew.bean.ItemStock;
import lance.honeydew.bean.OrderDetail;
import lance.honeydew.bean.OrderInformation;
import lance.honeydew.bean.UserMember;
import lance.honeydew.service.ShopService;
import lance.honeydew.util.ItemInformationUtil;

import net.sf.hibernate.collection.Set;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

public class IndexAction extends BaseAction {

	private static final Logger logger = Logger.getLogger(IndexAction.class);
	
	/*Bean*/
	private ShopService shopService;
	private ItemInformation itemInformationBo;
	private ItemStock itemStockBo;
	private ItemInformation itemInformationQb;
	private List<ItemInformation> itemInforList;
	private List<ItemStock> itemStockList;
	private Long bigSeqid;
	private Long medSeqid;
	private int itemCount;
	private Long itemSeqid;
	private int orderAmount;
	private String[] slidePhotoList;
	private Long itemStockSeqid;
	
	public ShopService getShopService() {
		return shopService;
	}


	public void setShopService(ShopService shopService) {
		this.shopService = shopService;
	}
	
	public ItemInformation getItemInformationBo() {
		return itemInformationBo;
	}

	public void setItemInformationBo(ItemInformation itemInformationBo) {
		this.itemInformationBo = itemInformationBo;
	}
	
	public ItemStock getItemStockBo() {
		return itemStockBo;
	}


	public void setItemStockBo(ItemStock itemStockBo) {
		this.itemStockBo = itemStockBo;
	}


	public ItemInformation getItemInformationQb() {
		return itemInformationQb;
	}


	public void setItemInformationQb(ItemInformation itemInformationQb) {
		this.itemInformationQb = itemInformationQb;
	}

	public List<ItemInformation> getItemInforList() {
		return itemInforList;
	}
	
	public void setItemInforList(List<ItemInformation> itemInforList) {
		this.itemInforList = itemInforList;
	}
	
	public List<ItemStock> getItemStockList() {
		return itemStockList;
	}


	public void setItemStockList(List<ItemStock> itemStockList) {
		this.itemStockList = itemStockList;
	}


	public Long getBigSeqid() {
		return bigSeqid;
	}


	public void setBigSeqid(Long bigSeqid) {
		this.bigSeqid = bigSeqid;
	}


	public Long getMedSeqid() {
		return medSeqid;
	}


	public void setMedSeqid(Long medSeqid) {
		this.medSeqid = medSeqid;
	}

	public int getItemCount() {
		return itemCount;
	}


	public void setItemCount(int itemCount) {
		this.itemCount = itemCount;
	}
	
	public Long getItemSeqid() {
		return itemSeqid;
	}


	public void setItemSeqid(Long itemSeqid) {
		this.itemSeqid = itemSeqid;
	}
	
	public int getOrderAmount() {
		return orderAmount;
	}

	public void setOrderAmount(int orderAmount) {
		this.orderAmount = orderAmount;
	}

	public String[] getSlidePhotoList() {
		return slidePhotoList;
	}


	public void setSlidePhotoList(String[] slidePhotoList) {
		this.slidePhotoList = slidePhotoList;
	}
	
	public Long getItemStockSeqid() {
		return itemStockSeqid;
	}


	public void setItemStockSeqid(Long itemStockSeqid) {
		this.itemStockSeqid = itemStockSeqid;
	}


	/*Action*/
	public String execute() {
		
		UserMember sessionUser = getSessionUserMember();
		
		if (sessionUser != null) {
			setUserMemberBo(sessionUser);
		} else {
			return ERROR;
		}
		
		return SUCCESS;
	}
	
	/**
	 * index.jsp
	 * @return
	 */
	public String index() {
		
		try {
			
			
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return "index";
	}
	
	/**
	 * index.jsp
	 * @return
	 */
	public String indexQuery() {
		
		try {
			
			itemCount = shopService.countItemInformationByIndexQuery(bigSeqid, medSeqid);
			double value = ((double) itemCount / (double) pageSize);
			pageMax = (int) Math.ceil(value);
			itemInforList = shopService.queryItemInformationByIndexQuery(bigSeqid, medSeqid, pageNum, pageSize);
			
			if (itemInforList != null) {
				for (int i = 0; i < itemInforList.size(); i++) {
					ItemInformation obj = itemInforList.get(i);
					if (obj.getItemStockSet() == null) {
						itemInforList.set(i, null);
					} 
					//無庫存
					else if (obj.getItemStockSet().size() == 0) {
						itemInforList.set(i, null);
					} 
					//商品下架
					else if (obj.getStartdate().equals(ItemInformationUtil.STATE_REMOVE_INT)) {
						itemInforList.set(i, null);
					}
				}
			}
			
		} catch(Exception e) {
			logger.debug(e);
		}

		return "indexQuery";
	}
	
	/**
	 * index_all.jsp
	 * @return
	 */
	public String indexQueryAll() {
		
		try {
			
			itemCount = shopService.countItemInformationByIndexQueryAll(searchAllName, bigSeqid, medSeqid);
			double value = ((double) itemCount / (double) pageSize);
			pageMax = (int) Math.ceil(value);
			itemInforList = shopService.queryItemInformationByIndexQueryAll(searchAllName, bigSeqid, medSeqid, pageNum, pageSize);
			
			if (itemInforList != null) {
				for (int i = 0; i < itemInforList.size(); i++) {
					ItemInformation obj = itemInforList.get(i);
					if (obj.getItemStockSet() == null) {
						itemInforList.set(i, null);
					} 
					//無庫存
					else if (obj.getItemStockSet().size() == 0) {
						itemInforList.set(i, null);
					} 
					//商品下架
					else if (obj.getStartdate().equals(ItemInformationUtil.STATE_REMOVE_INT)) {
						itemInforList.set(i, null);
					}
				}
			}
			
		} catch(Exception e) {
			logger.debug(e);
		}

		return "indexQueryAll";
	}
	
	/**
	 * index_inside.jsp
	 * @return
	 */
	public String indexInside() {
		
		try {
						
			itemInformationBo = shopService.findItemInformationByIndexInside(itemSeqid);
			
			if (itemInformationBo != null) {
				if (StringUtils.isNotEmpty(itemInformationBo.getSecondaryphoto())) {
					slidePhotoList = itemInformationBo.getSecondaryphoto().split(",");
				}
				
				if (itemInformationBo.getItemStockSet() != null) {
					itemStockList = new ArrayList(itemInformationBo.getItemStockSet());
				}
			}
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return "indexInside";
	}
	
	/**
	 * index_inside.jsp
	 * @return
	 */
	public String checkItemStock() {
		
		try {
			
			ItemStock itemStockObj = null;
			if (itemStockBo != null) {
				if (itemStockBo.getSeqid() != null) {
					itemStockObj = shopService.findItemStockBySeqId(itemStockBo.getSeqid());
				}
			}
			
			if (itemStockObj != null){
				
				if (itemStockObj.getAmount() > 0 && 
						itemStockObj.getAmount() > orderAmount) {
					jsonMessage = "true";
				} else {
					jsonMessage = "false";
				}
				
			} else {
				jsonMessage = "false";
			}
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return SUCCESS;
	}
	
	/**
	 * index_inside.jsp
	 * @return
	 */
	public String addShoppingCar() {
		
		try {
			
			ItemStock itemStockObj = null;
			if (itemStockBo != null) {
				if (itemStockBo.getSeqid() != null) {
					itemStockObj = shopService.findItemStockBySeqId(itemStockBo.getSeqid());
				}
			}
			
			if (itemStockObj != null){
				
				if (itemStockObj.getAmount() > 0) {
					
					itemStockObj.setAmount(itemStockObj.getAmount() - orderAmount);
					//塞值
					OrderDetail orderDetailObj = new OrderDetail();
					orderDetailObj.setAmount((long) orderAmount);
					orderDetailObj.setColor(itemStockObj.getColor());
					orderDetailObj.setIteminformationseqid(itemStockObj.getItemseqid());
					orderDetailObj.setPrice(itemStockObj.getItemseqid().getPrice());
					orderDetailObj.setSize(itemStockObj.getSize());
					
					OrderInformation orderInforObj = getSessionShoppingCar();
					//放到購物車中
					if (orderInforObj == null) {
						orderInforObj = new OrderInformation();
					}
					
					HashSet<OrderDetail> orderDetailSet;
					if (orderInforObj.getOrderDetailSet() == null) {
						orderDetailSet = new HashSet<OrderDetail>();
					} else {
						orderDetailSet = (HashSet<OrderDetail>) orderInforObj.getOrderDetailSet();
					}
					
					orderDetailSet.add(orderDetailObj);
					orderInforObj.setOrderDetailSet(orderDetailSet);
					
					//更新商品庫存
					shopService.updateItemStockByObj(itemStockObj);
					//更新session shopping car
					setSessionShoppingCar(orderInforObj);
					
					jsonMessage = "true";
				} else {
					jsonMessage = "false";
				}
				
			} else {
				jsonMessage = "false";
			}
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return SUCCESS;
	}
}
